package gc3d.griddata;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

import org.apache.commons.io.IOUtils;

/**
 * @author Marijn
 *
 */
public class ZipUtils {

   public static void unzipArchive(File archive, File outputDir) {
       try {
           ZipFile zipfile = new ZipFile(archive);
           for (Enumeration e = zipfile.entries(); e.hasMoreElements(); ) {
               ZipEntry entry = (ZipEntry) e.nextElement();
               unzipEntry(zipfile, entry, outputDir);
           }
           zipfile.close();
       } catch (Exception e) {
    	   e.printStackTrace();
       }
   }

   private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {

       if (entry.isDirectory()) {
           createDir(new File(outputDir, entry.getName()));
           return;
       }

       File outputFile = new File(outputDir, entry.getName());
       if (!outputFile.getParentFile().exists()){
           createDir(outputFile.getParentFile());
       }

       BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
       BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

       try {
           IOUtils.copy(inputStream, outputStream);
       } finally {
           outputStream.close();
           inputStream.close();
       }
   }

	private static void createDir(File dir) {
		if(!dir.mkdirs()) throw new RuntimeException("Can not create dir "+dir);
	}
}
